VARIABLE siteName VARCHAR2(100)
EXECUTE :siteName := '&1';

VARIABLE author VARCHAR2(100)
EXECUTE :author := '&2';

-- Fail if the test data is already loaded...
WHENEVER SQLERROR EXIT 1
INSERT INTO RA$Sites (
  siteName, isSecurityEnabled
) VALUES (
  :siteName, 'Y'
);

VARIABLE psetID_1 NUMBER
INSERT INTO RA$PermissionSets (
  siteName
) VALUES (
  :siteName
) RETURNING permissionSetID INTO :psetID_1;

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage
) VALUES (
  :psetID_1, 'User', 'Y'
);

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage, editPage, editPermissions
) VALUES (
  :psetID_1, 'Admin', 'Y', 'Y', 'Y'
);

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage
) VALUES (
  :psetID_1, 'external', 'Y'
);

VARIABLE psetID_2 NUMBER
INSERT INTO RA$PermissionSets (
  siteName
) VALUES (
  :siteName
) RETURNING permissionSetID INTO :psetID_2;

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage, editPage
) VALUES (
  :psetID_2, 'User', 'Y', 'Y'
);

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage, editPage, editPermissions,
  createPage, createFolder
) VALUES (
  :psetID_2, 'Admin', 'Y', 'Y', 'Y',
  'Y', 'Y'
);

VARIABLE psetID_3 NUMBER
INSERT INTO RA$PermissionSets (
  siteName
) VALUES (
  :siteName
) RETURNING permissionSetID INTO :psetID_3;

INSERT INTO RA$Permissions (
  permissionSetID, roleName, viewPage, editPage, editPermissions
) VALUES (
  :psetID_3, 'Admin', 'Y', 'Y', 'Y'
);

VARIABLE folderID_1 NUMBER
INSERT INTO RA$Folders (
  siteName, path, permissionSetID
) VALUES (
  :siteName, '/', :psetID_1
) RETURNING folderID INTO :folderID_1;

VARIABLE folderID_2 NUMBER
INSERT INTO RA$Folders (
  siteName, parentFolderID, path, permissionSetID
) VALUES (
  :siteName, :folderID_1, '/Folder1/', :psetID_2
) RETURNING folderID INTO :folderID_2;

VARIABLE folderID_3 NUMBER
INSERT INTO RA$Folders (
  siteName, parentFolderID, path, permissionSetID
) VALUES (
  :siteName, :folderID_1, '/Folder2/', :psetID_2
) RETURNING folderID INTO :folderID_3;

VARIABLE pageID_1 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision
) VALUES (
  :folderID_1, 'faq', 1
) RETURNING pageID INTO :pageID_1;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_1, 1, :author, 'Frequently Asked Questions',
  '<h2>Questions</h2>
<p>These are some answers.</p>'
);

VARIABLE pageID_2 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, permissionSetID, headRevision
) VALUES (
  :folderID_1, 'test', :psetID_3, 1
) RETURNING pageID INTO :pageID_2;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_2, 1, :author, 'Test',
  '<h2>Test</h2>
<p>This page does not have inherited permissions.</p>'
);

VARIABLE pageID_3 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision
) VALUES (
  :folderID_2, 'Default', 1
) RETURNING pageID INTO :pageID_3;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_3, 1, :author, 'Folder 1 Default',
  '<h2>Folder 1 Default</h2>
<p>This is <b>content</b>.</p>'
);

VARIABLE pageID_4 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision
) VALUES (
  :folderID_3, 'Default', 1
) RETURNING pageID INTO :pageID_4;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_4, 1, :author, 'Folder 2 Default',
  '<h2>Folder 2 Default</h2>
<p>This is <b>content</b>.</p>'
);

VARIABLE pageID_5 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision
) VALUES (
  :folderID_1, 'Default', 1
) RETURNING pageID INTO :pageID_5;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_5, 1, :author, 'Welcome to the Wiki!',
  '<h2>Header</h2>
<p>This is some text.</p>
<p>{[link:1]}</p>
<p>{[link:2]}</p>
<ul>
<li>{[link:3]}</li>
<li>{[link:4]}</li>
</ul>'
);

INSERT INTO RA$PageLinks (
  pageID, revisionNumber, pageLinkKey, displayText, linkPageID
) VALUES (
  :pageID_5, 1, 1, 'FAQ', :pageID_1
);

INSERT INTO RA$PageLinks (
  pageID, revisionNumber, pageLinkKey, displayText, linkPageID
) VALUES (
  :pageID_5, 1, 2, 'Test', :pageID_2
);

INSERT INTO RA$PageLinks (
  pageID, revisionNumber, pageLinkKey, displayText, linkPageID
) VALUES (
  :pageID_5, 1, 3, 'Folder 1', :pageID_3
);

INSERT INTO RA$PageLinks (
  pageID, revisionNumber, pageLinkKey, displayText, linkPageID
) VALUES (
  :pageID_5, 1, 4, 'Folder 2', :pageID_4
);

VARIABLE pageID_6 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision, retentionUnit, retentionMeasure
) VALUES (
  :folderID_1, 'Versioned', 1, 'Versions', 2
) RETURNING pageID INTO :pageID_6;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_6, 1, :author, 'Versioned Page',
  '<h2>Versioned Page</h2>
<p>This is content.</p>'
);

VARIABLE pageID_7 NUMBER
INSERT INTO RA$Pages (
  folderID, pageName, headRevision, retentionUnit, retentionMeasure
) VALUES (
  :folderID_1, 'Day-Versioned', 1, 'Days', 15
) RETURNING pageID INTO :pageID_7;

INSERT INTO RA$PageRevisions (
  pageID, revisionNumber, revisionAuthor, pageTitle,
  pageContent
) VALUES (
  :pageID_7, 1, :author, 'Day Versioned Page',
  '<h2>Day Versioned Page</h2>
<p>This is content.</p>'
);